#include "Defs.h"
#include "Utils.h"
#include "Exception.h"


// 
// Retrieve ServerVariable (filter)
//

void Utils_T::GetServerVariable(PHTTP_FILTER_CONTEXT pFC, const char * szName, String_T & str)
{
	DWORD dw = 0;

	if( !pFC->GetServerVariable(pFC, (LPSTR)szName, NULL, &dw) )
	{
		DWORD dwErr = GetLastError();

		if( ERROR_INVALID_INDEX == dwErr )
		{
			return;
		}

		else if( ERROR_INSUFFICIENT_BUFFER != dwErr )
		{
			throw Exception_T(dwErr, "Error %d GetServerVariable(filter, %s) - %s - %d - %s", dwErr, szName, __FILE__, __LINE__, __FUNCSIG__);
		}
	}

	str.Create(dw - 1);

	if( !pFC->GetServerVariable(pFC, (LPSTR)szName, str.Ptr(), &dw) )
	{
		throw Exception_T(GetLastError(), "Error %d GetServerVariable(filter, %s) - %s - %d - %s", GetLastError(), szName, __FILE__, __LINE__, __FUNCSIG__);
	}
}

// 
// Retrieve ServerVariable (extension)
//

void Utils_T::GetServerVariable(LPEXTENSION_CONTROL_BLOCK pECB, const char * szName, String_T & str)
{
	DWORD dw = 0;

	if( !pECB->GetServerVariable(pECB, (LPSTR)szName, NULL, &dw) )
	{
		DWORD dwErr = GetLastError();

		if( ERROR_INVALID_INDEX == dwErr )
		{
			return;
		}

		else if( ERROR_INSUFFICIENT_BUFFER != dwErr )
		{
			throw Exception_T(dwErr, "Error %d GetServerVariable(extension, %s) - %s - %d - %s", dwErr, szName, __FILE__, __LINE__, __FUNCSIG__);
		}
	}

	str.Create(dw - 1);

	if( !pECB->GetServerVariable(pECB, (LPSTR)szName, str.Ptr(), &dw) )
	{
		throw Exception_T(GetLastError(), "Error %d GetServerVariable(extension, %s) - %s - %d - %s", GetLastError(), szName, __FILE__, __LINE__, __FUNCSIG__);
	}
}

// 
// Retrieve Header (filter)
//

void Utils_T::GetHeader(PHTTP_FILTER_CONTEXT pFC, PHTTP_FILTER_PREPROC_HEADERS pHeaders, const char * szName, String_T & str)
{
	DWORD dw = 0;

	if( !pHeaders->GetHeader(pFC, (LPSTR)szName, NULL, &dw) )
	{
		DWORD dwErr = GetLastError();

		if( ERROR_INVALID_INDEX == dwErr )
		{
			return;
		}

		else if( ERROR_INSUFFICIENT_BUFFER != dwErr )
		{
			throw Exception_T(dwErr, "Error %d GetHeader(%s) - %s - %d - %s", dwErr, szName, __FILE__, __LINE__, __FUNCSIG__);
		}
	}

	str.Create(dw - 1);

	if( !pHeaders->GetHeader(pFC, (LPSTR)szName, str.Ptr(), &dw) )
	{
		throw Exception_T(GetLastError(), "Error %d GetHeader(%s) - %s - %d - %s", GetLastError(), szName, __FILE__, __LINE__, __FUNCSIG__);
	}
}

// 
// Reads the contents of a file to a buffer
//

void Utils_T::GetFileContents(const char * szFilename, Buffer_T & buf)
{
	char szExpandedString[MAX_PATH];

	// 
	// Expand any existing environment variables
	//
	
	DWORD dwExpanded = ExpandEnvironmentStrings(szFilename, szExpandedString, sizeof(szExpandedString));
    if( (dwExpanded > sizeof(szExpandedString)) || (dwExpanded == 0) )
	{
		DWORD dwErr = GetLastError();

		if( dwExpanded > 0 )
		{
			dwErr = ERROR_INSUFFICIENT_BUFFER;
		}

		throw Exception_T(dwErr, "Error %d ExpandEnvironmentStrings(%s) - %s - %d - %s", dwErr, szFilename, __FILE__, __LINE__, __FUNCSIG__);
	}

	// 
	// Read file contents
	//

	File_T file(szExpandedString, GENERIC_READ, FILE_SHARE_READ , OPEN_EXISTING, false);
	buf.Create(File_T::GetSize(szExpandedString));
	file.Read(buf.Ptr(), buf.Max());
}

//
// Creates all needed parent directories of
// directory given as argument
//

void Utils_T::CreateParentDirs(char * szPath)
{
	for( int ix = 3 ; szPath[ix] != '\0' ; ix++ )
	{
		if( szPath[ix] == '\\' )
		{
			szPath[ix] = '\0';
			if( !CreateDirectory(szPath, NULL) )
			{
				if( GetLastError() != ERROR_ALREADY_EXISTS )
				{
					throw Exception_T(GetLastError(), "Error %d CreateDirectory(%s) - %s - %d - %s", GetLastError(), szPath, __FILE__, __LINE__, __FUNCSIG__);
				}
			}
			szPath[ix] = '\\';
		}
	}
}
